
var app = getApp();
var http_service = app.data.service;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    service:http_service,
    orderId:0,
    orderInfo:{},
    minute:'05',
    second:'00',
    seconds:300,
    user_quan_id:0,
    click:true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var orderId =  options.orderId;
    var that = this;
    that.setData({
      orderId:orderId,
      seconds:300
    })
    wx.request({
      url: http_service +'/index.php/findorderinfo',
      data:{
        orderId:orderId
      },
      success:function(res){
        that.setData({
          orderInfo: res.data
        })
      }
    })

    /**
     * show
     */
    var that = this;
    //将隐藏设置为true
    app.data.hide = true
    app.data.orderId = that.data.orderId
    //倒计时
    setInterval(function () {
      var s1 = that.data.seconds;
      s1 = s1 - 1;
      //订单过期
      if (s1 == 0) {
        //过期之后将全局订单编号更改
        app.data.orderId = 0;
        wx.showLoading({
          title: '订单过期',
        })
        wx.request({
          url: http_service + '/index.php/apideleteorder',
          data: {
            orderId: that.data.orderId
          }
        })
        setTimeout(function () {
          wx.hideLoading()
          wx.reLaunch({
            url: '/pages/index/subscribe/childservice1'
          })
        }, 500)

      }
      var m = Math.floor(s1 / 60);
      var s = s1 % 60;
      if (s < 10) {
        s = '0' + s;
      }

      that.setData({
        minute: '0' + m,
        second: s,
        seconds: s1
      })
    }, 1000)

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    /*var that = this;
    //将隐藏设置为true
    app.data.hide= true
    app.data.orderId = that.data.orderId
    //倒计时
    setInterval(function () {
      var s1 = that.data.seconds;
      s1 = s1-1;
      //订单过期
      if (s1==0){
        //过期之后将全局订单编号更改
        app.data.orderId=0;
        wx.showLoading({
          title: '订单过期',
        })
        wx.request({
          url: http_service +'/index.php/apideleteorder',
          data:{
            orderId:that.data.orderId
          }
        })
        setTimeout(function () {
          wx.hideLoading()
          wx.reLaunch({
            url: '/pages/index/subscribe/service'
          })
        }, 500)
       
      }
      var m = Math.floor(s1/60);
      var s = s1%60;
      if (s<10){
        s = '0'+s;
      }
      
      that.setData({
        minute: '0'+m,
        second: s,
        seconds: s1
      })
    }, 1000)*/

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
   
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  },

  /**
   * 点击支付
   */
  pay:function(){
    var that = this;
    that.setData({
      click: false
    })
    if(!that.data.click){
      wx.request({
        url: http_service + '/index.php/wxpay1',
        data: {
          orderId: that.data.orderId,
          user_quan_id: that.data.user_quan_id
        },
        success: function (res) {
          if (res.data.state == 0) {
            wx.showModal({
              title: '预约失败',
              content: res.data.text,
            })
          }
          if (res.data.state == 1) {
            var prepay_id = res.data['prepay_id'];
            //console.log(prepay_id);
            wx.requestPayment({
              timeStamp: res.data['timeStamp'],
              nonceStr: res.data['nonceStr'],
              package: res.data['package'],
              signType: 'MD5',
              paySign: res.data['paySign'],
              'fail': function () {
                that.setData({
                  click: true
                })
              },
              'success': function (res) {
                //用户已经支付成功

                //两个全局变量都更改为初始状态
                app.data.hide = false;
                app.data.orderId = 0;
                
                wx.request({
                  url: http_service +'/index.php/payokcallback',
                  data:{
                    prepay_id:prepay_id,
                    orderId:that.data.orderId,
                    user_quan_id:that.data.user_quan_id
                  },
                  success:function(res){
                    wx.showLoading({
                      title: '订单下单成功！',
                    })
                    setTimeout(function () {
                      wx.hideLoading()
                      wx.reLaunch({
                        url: '/pages/index/subscribe/childservice1'
                      })
                    }, 1000)
                  }
                })
              }
            })
          }
        },
        fail: function () {
          console.log("失败");
        }
      })
    }
   

  },

  /**
   * 选择优惠券
   */
  radioChange: function (e) {
    //console.log('radio发生change事件，携带value值为：', e.detail.value);
    var that = this;
    that.setData({
      user_quan_id:e.detail.value
    })
  }
})